package com.uetoken.cn.utils;


import android.content.Context;
import android.location.LocationManager;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.blankj.utilcode.util.LogUtils;
import com.uetoken.cn.base.MyApplication;
import com.uetoken.cn.bean.LocationBean;

import org.greenrobot.eventbus.EventBus;

/**
 * 获取当前定位
 */
public class GetCurrentLocation {
    private static LocationClient mLocationClient;

    private static LocationClient getLocationClientInstance() {
        if (mLocationClient == null) {
            mLocationClient = new LocationClient(MyApplication.getInstance());
            LocationClientOption option = new LocationClientOption();
            //可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
            option.setCoorType("bd09ll");
            //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
            option.setIgnoreKillProcess(false);
            option.setEnableSimulateGps(false);
            //可选，设置是否需要地址信息，默认不需要
            option.setIsNeedAddress(true);
            mLocationClient.setLocOption(option);
        }
        return mLocationClient;
    }

    /**
     * 开启定位，获取当前位置
     *
     */
    public static void getLocationCity() {
        try {
            //打开百度定位，获取经纬度
            final LocationClient mLocationClient = getLocationClientInstance();
            mLocationClient.registerLocationListener(mBDAbstractLocationListener);
            //开启定位
            mLocationClient.start();
        } catch (Exception e) {
            mLocationClient.stop();
        }
    }

   private static BDAbstractLocationListener mBDAbstractLocationListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //定位到就停止mLocationClient
            mLocationClient.stop();
            LogUtils.e(bdLocation.getLongitude(),bdLocation.getLatitude());
            EventBus.getDefault().post(new LocationBean(String.valueOf(bdLocation.getLongitude()),String.valueOf(bdLocation.getLatitude())));
        }
        @Override
        public void onLocDiagnosticMessage(int i, int i1, String s) {
            super.onLocDiagnosticMessage(i, i1, s);
            mLocationClient.stop();
            EventBus.getDefault().post(new LocationBean("0","0"));
            if (i1==1 || i1==2 || i1==3||i1==4 || i1==5 || i1==6||i1==7 || i1==8 || i1==9){
                //定位失败
            }
        }
    };



    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     * @param context
     * @return true 表示开启
     */
    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    public static void destory(){
        if (mLocationClient!=null){
            mLocationClient.unRegisterLocationListener(mBDAbstractLocationListener);
        }
    }
}
